package com.hrt.manage.stock.service.impl;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.print.PrintException;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.StaleObjectStateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.BackGoods;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftInfor;
import com.hrt.bean.OrderDetail;
import com.hrt.bean.OrderInfor;
import com.hrt.bean.ProductDetails;
import com.hrt.bean.ProductInfor;
import com.hrt.bean.ResourceType;
import com.hrt.bean.SnCodeSequence;
import com.hrt.bean.StockDetail;
import com.hrt.bean.Stocks;
import com.hrt.bean.StocksAction;
import com.hrt.manage.bkgoods.service.BackGoodsService;
import com.hrt.manage.order.service.OrderDetailService;
import com.hrt.manage.product.service.GiftDetailService;
import com.hrt.manage.product.service.GiftInforService;
import com.hrt.manage.product.service.ProDetailsService;
import com.hrt.manage.product.service.ProInforService;
import com.hrt.manage.sequence.service.SequenceService;
import com.hrt.manage.sncode.service.SNCodeService;
import com.hrt.manage.stock.dao.StockDao;
import com.hrt.manage.stock.dao.StockDetailDao;
import com.hrt.manage.stock.service.StocksService;
import com.hrt.thirtyparty.sncode.SNCodePrinter;
import com.hrt.vo.OrderDetailVO;
import com.hrt.vo.StockDetailVO;
import com.hrt.vo.StocksVO;

@Service
public class StockServiceImpl implements StocksService {

	@Autowired
	private StockDao dao;

	@Autowired
	private StockDetailDao detailDao;

	@Autowired
	private ProDetailsService proDetailService;

	@Autowired
	private SNCodeService snCodeService;

	@Autowired
	private ProInforService proInforService;

	@Autowired
	private GiftDetailService giftDetailService;

	@Autowired
	private GiftInforService giftInforService;

	@Autowired
	private OrderDetailService orderDetailService;

	@Autowired
	private BackGoodsService backGoodService;

	@Autowired
	private SequenceService seqService;

	@Override
	public List<Stocks> getStockByType(Integer reType, Long proId) {
		// TODO Auto-generated method stub
		List<Stocks> list = new ArrayList<Stocks>();
		list = dao.getStockByType(reType, proId);
		return list;
	}

	@Override
	public List<Stocks> getStockByType(Long proId, Integer reType,
			Integer pageIndex, Integer pageSize) {

		List<Stocks> list = new ArrayList<Stocks>();
		list = dao.getStockByType(reType, proId, pageIndex, pageSize);
		return list;
	}

	@Override
	public List<Stocks> getProductByReType(Integer retype, Integer pageIndex,
			Integer pageSize) {
		// TODO Auto-generated method stub
		List<Stocks> list = new ArrayList<Stocks>();
		list = dao.getProductByReType(retype, pageIndex, pageSize);
		return list;
	}

	@Override
	public List<Stocks> getProductByAction(Integer actStr, Integer pageIndex,
			Integer pageSize) {
		// TODO Auto-generated method stub
		List<Stocks> list = new ArrayList<Stocks>();
		list = dao.getProductByAction(actStr, pageIndex, pageSize);
		return list;
	}

	@Override
	public List<Stocks> getStockList(Integer retype, Integer actStr,
			Integer pageIndex, Integer pageSize) {
		// TODO Auto-generated method stub
		List<Stocks> list = new ArrayList<Stocks>();
		list = dao.getStockList(retype, actStr, pageIndex, pageSize);
		return list;
	}

	@Override
	public Long totalCount(Integer reType) {
		// TODO Auto-generated method stub
		return dao.totalCount(reType);
	}

	@Override
	@Transactional
	public Stocks saveStocks(StocksVO vo) {
		// TODO Auto-generated method stub
		Stocks stocks = new Stocks();
		List<StockDetail> delist = new ArrayList<StockDetail>();
		try {
			BeanUtils.copyProperties(stocks, vo);

			for (StockDetailVO detailVO : vo.getDetails()) {
				StockDetail newDetail = new StockDetail();
				BeanUtils.copyProperties(newDetail, detailVO);
				delist.add(newDetail);
			}

		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Long stocksId = dao.saveStocks(stocks);
		Stocks newBean = dao.loadById(Stocks.class, stocksId);
		saveDetail(newBean, delist);

		return newBean;

	}

	protected boolean confirmStock(Stocks stock) {
		// TODO Auto-generated method stub
		Long stockId = stock.getId();
		List<StockDetail> delist = new ArrayList<StockDetail>();
		delist = detailDao.getDetailsByStockId(stockId);
		int count = 0;
		// 进库
		for (StockDetail detail : delist) {

			if (StocksAction.ENTER_STOCKS == stock.getAction()) {
				enterStocks(detail, stock.getReType());
			}
			// 出库
			if (StocksAction.OUT_STOCKS == stock.getAction()) {
				outStocks(detail, stock.getReType());
			}
			// 退货入库
			if (StocksAction.BACK_STOCKS == stock.getAction()) {
				backStocks(detail, stock.getReType());
			}
			// 退货出库
			if (StocksAction.BACK_PROVIDER == stock.getAction()) {
				exitStocks(detail, stock.getReType());
			}
			count++;
		}
		if (count == delist.size()) {
			return true;
		} else {
			return false;
		}
	}

	protected boolean exitStocks(StockDetail detail, Integer reType) {
		Long deTotal = detail.getTotal();
		// 产品/礼品ID
		Long proId = detail.getProId();
		// 规格ID
		Long ruleId = detail.getRuleId();

		if (reType == ResourceType.PRODUCT_RESOURCE) {
			ProductDetails proDetails = this.proDetailService
					.getDetailByProIdAndRuleId(proId, ruleId);
			Long oldTotal = proDetails.getTotal().longValue();
			if (oldTotal - deTotal < 0) {
				return false;
			} else {
				proDetails.setTotal(oldTotal.longValue() - deTotal.longValue());
				try {
					proDetailService.udateDetail(proDetails);
					ProductInfor product = proDetails.getProductInfor();
					proInforService.computeTotal(product);
					return true;
				} catch (StaleObjectStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}

		}
		if (reType == ResourceType.GIFT_RESOURCE) {

			GiftDetail giftDetails = this.giftDetailService
					.getDetailByGiftIdAndRuleId(proId, ruleId);
			giftDetails.setTotals(giftDetails.getTotals().longValue()
					+ deTotal.longValue());

		}
		return false;

	}

	protected boolean backStocks(StockDetail detail, Integer reType) {
		Long deTotal = detail.getTotal();
		// 产品/礼品ID
		Long proId = detail.getProId();
		// 规格ID
		Long ruleId = detail.getRuleId();

		if (reType == ResourceType.PRODUCT_RESOURCE) {

			ProductDetails proDetails = this.proDetailService
					.getDetailByProIdAndRuleId(proId, ruleId);
			Long oldTotal = proDetails.getTotal().longValue();
			if (oldTotal - deTotal < 0) {
				return false;
			} else {
				proDetails.setTotal(oldTotal.longValue() - deTotal.longValue());
				try {
					proDetailService.udateDetail(proDetails);
					ProductInfor product = proDetails.getProductInfor();
					proInforService.computeTotal(product);
					return true;
				} catch (StaleObjectStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}

		}
		if (reType == ResourceType.GIFT_RESOURCE) {

			GiftDetail giftDetails = this.giftDetailService
					.getDetailByGiftIdAndRuleId(proId, ruleId);
			giftDetails.setTotals(giftDetails.getTotals().longValue()
					+ deTotal.longValue());
			GiftInfor giftInfor = giftDetails.getGiftInfor();
			this.giftInforService.computeTotal(giftInfor);
			return true;

		}
		return false;

	}

	/**
	 * 产品/礼品 出库操作
	 * 
	 * @param detail
	 * @param reType
	 */
	protected boolean outStocks(StockDetail detail, Integer reType) {
		Long deTotal = detail.getTotal();
		// 产品/礼品ID
		Long proId = detail.getProId();
		// 规格ID
		Long ruleId = detail.getRuleId();

		if (reType == ResourceType.PRODUCT_RESOURCE) {
			ProductDetails proDetails = this.proDetailService
					.getDetailByProIdAndRuleId(proId, ruleId);
			Long oldTotal = proDetails.getTotal().longValue();
			if (oldTotal - deTotal < 0) {
				return false;
			} else {
				proDetails.setTotal(oldTotal.longValue() - deTotal.longValue());
				try {
					proDetailService.udateDetail(proDetails);
					ProductInfor product = proDetails.getProductInfor();
					proInforService.computeTotal(product);
					return true;
				} catch (StaleObjectStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}

		}
		if (reType == ResourceType.GIFT_RESOURCE) {

			GiftDetail giftDetails = this.giftDetailService
					.getDetailByGiftIdAndRuleId(proId, ruleId);
			Long oldTotal = giftDetails.getTotals().longValue();
			if (oldTotal - deTotal < 0) {
				return false;
			} else {
				giftDetails.setTotals(giftDetails.getTotals().longValue()
						- deTotal.longValue());
				this.giftDetailService.updateGiftDetail(giftDetails);
				GiftInfor giftInfor = giftDetails.getGiftInfor();
				giftInforService.computeTotal(giftInfor);
				return true;
			}

		}
		return false;

	}

	/**
	 * 产品/礼品 入库
	 * 
	 * @param detail
	 * @param reType
	 */
	protected boolean enterStocks(StockDetail detail, Integer reType) {

		SNCodePrinter snCodePrinter = new SNCodePrinter();
		List<File> snCodeImgs=new ArrayList<File>();
		Long deTotal = detail.getTotal();
		// 产品/礼品ID
		Long proId = detail.getProId();
		// 规格ID
		Long ruleId = detail.getRuleId();

		if (reType == ResourceType.PRODUCT_RESOURCE) {
			ProductDetails proDetails = this.proDetailService
					.getDetailByProIdAndRuleId(proId, ruleId);
			proDetails.setTotal(proDetails.getTotal().longValue()
					+ deTotal.longValue());
			try {
				proDetailService.udateDetail(proDetails);
				ProductInfor product = proDetails.getProductInfor();
				proInforService.computeTotal(product);
				List<SnCodeSequence> snCodes = snCodeService.genarateSnCodeByCode(
						detail, ResourceType.PRODUCT_RESOURCE);
				if (snCodes != null && !snCodes.isEmpty()) {
					for (int i = 0; i < snCodes.size(); i++) {
						Long snCodeId = snCodeService.saveSnCode(snCodes.get(i));
						if (snCodeId != null) {
							try {
								File file=snCodePrinter.genarateSNCoe(snCodes.get(i).getSnCode());
								if(file!=null && file.exists()){
									snCodeImgs.add(file);
								}
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

					}
					if(!snCodeImgs.isEmpty()){
						try {
							snCodePrinter.printSNCode(snCodeImgs);
						} catch (PrintException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					return true;
				} else {
					return false;
				}
			} catch (StaleObjectStateException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InvocationTargetException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			

		}
		if (reType == ResourceType.GIFT_RESOURCE) {

			GiftDetail giftDetails = this.giftDetailService
					.getDetailByGiftIdAndRuleId(proId, ruleId);
			giftDetails.setTotals(giftDetails.getTotals().longValue()
					+ deTotal.longValue());
			this.giftDetailService.updateGiftDetail(giftDetails);
			GiftInfor giftInfor = giftDetails.getGiftInfor();
			giftInforService.computeTotal(giftInfor);
			List<SnCodeSequence> snCodes = snCodeService.genarateSnCodeByCode(
					detail, ResourceType.GIFT_RESOURCE);

			if (snCodes != null && !snCodes.isEmpty()) {
				for (int i = 0; i < snCodes.size(); i++) {
					snCodeService.saveSnCode(snCodes.get(i));
					Long snCodeId = snCodeService.saveSnCode(snCodes.get(i));
					if (snCodeId != null) {
						try {
							File file=snCodePrinter.genarateSNCoe(snCodes.get(i).getSnCode());
							if(file!=null && file.exists())
							{
								snCodeImgs.add(file);
							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				if(!snCodeImgs.isEmpty()){
					try {
						snCodePrinter.printSNCode(snCodeImgs);
					} catch (PrintException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				return true;
			} else {
				return false;
			}

		}
		return false;
	}

	@Override
	public Timestamp getDate() {
		// TODO Auto-generated method stub
		return dao.getDate();
	}

	@Transactional
	@Override
	public void saveDetail(Stocks newBean, List<StockDetail> detailsList) {
		// TODO Auto-generated method stub
		for (StockDetail d : detailsList) {
			d.setStock(newBean);
			this.detailDao.saveOrUpdateEntity(d);
		}

	}

	@Transactional
	@Override
	public void updateDetail(Stocks newBean, List<StockDetail> detailsList) {
		// TODO Auto-generated method stub
		for (StockDetail d : detailsList) {
			d.setStock(newBean);
			this.detailDao.saveOrUpdateEntity(d);
		}

	}

	@Override
	public StocksVO getStockById(Long id) {
		StocksVO vo = new StocksVO();
		Stocks stocks = dao.getById(id);
		List<StockDetailVO> detailsVO = new ArrayList<StockDetailVO>();
		List<StockDetail> details = detailDao.getDetailsByStockId(stocks
				.getId());

		try {
			BeanUtils.copyProperties(vo, stocks);
			for (StockDetail detail : details) {
				StockDetailVO dvo = new StockDetailVO();

				dvo.setId(detail.getId());
				dvo.setCreateTime(detail.getCreateTime());
				dvo.setCreator(detail.getCreator());
				dvo.setEditor(detail.getEditor());
				dvo.setEditTime(detail.getEditTime());
				dvo.setEnterPrice(detail.getEnterPrice());
				dvo.setIsdelete(detail.getIsdelete());
				dvo.setNote(detail.getNote());
				dvo.setRuleCode(detail.getRuleCode());
				dvo.setRuleName(detail.getRuleName());
				dvo.setRuleId(detail.getRuleId());
				dvo.setProCode(detail.getProCode());
				dvo.setProId(detail.getProId());
				dvo.setProName(detail.getProName());
				dvo.setReType(detail.getReType());
				dvo.setSalePrice(detail.getSalePrice());
				dvo.setSummery(detail.getSummery());
				dvo.setTotal(detail.getTotal());

				detailsVO.add(dvo);
			}
			vo.setDetails(detailsVO);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return vo;
	}

	@Override
	@Transactional
	public boolean updateStock(StocksVO vo) {
		Stocks stock = dao.getById(vo.getId());
		try {
			BeanUtils.copyProperties(stock, vo);
			List<StockDetailVO> detailVos = vo.getDetails();

			// List<StockDetail> details
			// =detailDao.getDetailsByStockId(stock.getId());
			List<StockDetail> details = new ArrayList<StockDetail>();

			for (StockDetailVO detail : detailVos) {
				StockDetail dEntry = null;
				if (detail == null) {
					continue;
				}

				if (detail.getId() != null) {
					dEntry = detailDao.getById(detail.getId());
				} else {
					dEntry = new StockDetail();
				}
				System.out.println("///////////same " + detail.getId());
				dEntry.setCreateTime(detail.getCreateTime());
				dEntry.setCreator(detail.getCreator());
				dEntry.setEditor(detail.getEditor());
				dEntry.setEditTime(detail.getEditTime());
				dEntry.setEnterPrice(detail.getEnterPrice());
				dEntry.setIsdelete(detail.getIsdelete());
				dEntry.setNote(detail.getNote());
				dEntry.setRuleCode(detail.getRuleCode());
				dEntry.setRuleName(detail.getRuleName());
				dEntry.setRuleId(detail.getRuleId());
				dEntry.setStock(stock);
				dEntry.setProCode(detail.getProCode());
				dEntry.setProId(detail.getProId());
				dEntry.setProName(detail.getProName());
				dEntry.setReType(detail.getReType());
				dEntry.setSalePrice(detail.getSalePrice());
				dEntry.setSummery(detail.getSummery());
				dEntry.setTotal(detail.getTotal());

				details.add(dEntry);
				if (detail.getSnCodes() != null
						&& !detail.getSnCodes().isEmpty()) {
					updateSNCode(detail.getSnCodes());
				}

			}

			updateDetail(stock, details);

			dao.updateEntity(stock);

			if (stock.getStatus() == 1) {
				if (confirmStock(stock)) {
					return true;
				}
				return false;
			}

		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	private void updateSNCode(List<SnCodeSequence> snCodes) {
		// TODO Auto-generated method stub
		for (SnCodeSequence snCode : snCodes) {
			this.snCodeService.updateSnCode(snCode);
		}

	}

	@Transactional
	@Override
	public void deleteById(Long[] idArr) {
		for (Long id : idArr) {
			List<StockDetail> details = this.detailDao.getDetailsByStockId(id);
			for (StockDetail detail : details) {
				detail.setIsdelete(new Integer(1));
				detailDao.updateEntity(detail);
			}
			Stocks stock = this.dao.getById(id);
			stock.setIsdelete(new Integer(1));
			dao.updateEntity(stock);
		}

	}

	@Override
	public List<StockDetailVO> getDetialsByStockId(Long stockId) {
		// TODO Auto-generated method stub
		List<StockDetail> list = dao.getDetialsByStockId(stockId);
		List<StockDetailVO> vos = new ArrayList<StockDetailVO>();
		for (StockDetail d : list) {
			StockDetailVO vo = new StockDetailVO();
			vo.setCreateTime(d.getCreateTime());
			vo.setCreator(d.getCreator());
			vo.setEditor(d.getEditor());
			vo.setEditTime(d.getEditTime());
			vo.setEnterPrice(d.getEnterPrice());
			vo.setId(d.getId());
			vo.setIsdelete(d.getIsdelete());
			vo.setNote(d.getNote());
			vo.setProCode(d.getProCode());
			vo.setProId(d.getProId());
			vo.setProName(d.getProName());
			vo.setReType(d.getReType());
			vo.setSalePrice(d.getSalePrice());
			vo.setCreateTime(d.getCreateTime());
			vo.setCreateTime(d.getCreateTime());
			vo.setSummery(d.getSummery());
			vo.setTotal(d.getTotal());
			vo.setRuleCode(d.getRuleCode());
			vo.setRuleName(d.getRuleName());
			vo.setRuleId(d.getRuleId());
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public StocksVO transOrderToStock(OrderInfor order) {
		// TODO Auto-generated method stub

		if (order != null && order.getId() != null) {
			StocksVO stock = new StocksVO();
			List<StockDetailVO> stockDetails = new ArrayList<StockDetailVO>();
			List<OrderDetail> orderDetails = orderDetailService
					.getOrderDetailByOrderId(order.getId());

			if (orderDetails != null && !orderDetails.isEmpty()) {
				List<OrderDetailVO> orderDetailVos = transferOrderDetailToVo(orderDetails);
				for (OrderDetailVO orderDetail : orderDetailVos) {
					if (orderDetail != null && orderDetail.getId() != null) {
						StockDetailVO stockDetail = new StockDetailVO();
						stockDetail.setProCode(orderDetail.getGiftCode());
						stockDetail.setProId(orderDetail.getGiftId());
						stockDetail.setProName(orderDetail.getGiftName());
						stockDetail.setTotal(new Long(orderDetail.getTotal()));
						stockDetail.setSalePrice(orderDetail.getSalePrice());
						stockDetail.setSummery(orderDetail.getSummery());
						stockDetail.setNote(orderDetail.getNote());
						stockDetail.setIsdelete(0);
						stockDetail.setReType(ResourceType.GIFT_RESOURCE);
						stockDetail.setRuleCode(orderDetail.getGdCode());
						stockDetail.setRuleId(orderDetail.getGiftDetailId());
						stockDetail.setRuleName(orderDetail.getGdName());
						stockDetail.setCreateTime(dao.getDate());
						stockDetail.setEditTime(dao.getDate());
						stockDetail.setCreator(order.getSender());
						stockDetail.setEditor(order.getSender());
						stockDetails.add(stockDetail);
					}
				}
			}
			stock.setStockCode(seqService.getNextSequence(Stocks.class));
			stock.setAction(StocksAction.OUT_STOCKS);
			stock.setReType(ResourceType.GIFT_RESOURCE);
			stock.setSummery(order.getSummery());
			stock.setEditor(order.getNote());
			stock.setCreateTime(dao.getDate());
			stock.setEditTime(dao.getDate());
			stock.setComfirmTime(dao.getDate());
			stock.setIsFromOrder(new Integer(1));
			stock.setOrderId(order.getId());
			stock.setOrderCode(order.getOrderCode());
			stock.setCreator(order.getSender());
			stock.setEditor(order.getSender());
			stock.setDetails(stockDetails);
			stock.setStatus(new Integer(0));
			return stock;
		}
		return null;
	}

	private List<OrderDetailVO> transferOrderDetailToVo(
			List<OrderDetail> orderDetails) {
		// TODO Auto-generated method stub
		List<OrderDetailVO> result = new ArrayList<OrderDetailVO>();
		if (orderDetails != null && !orderDetails.isEmpty()) {
			for (OrderDetail orderDetail : orderDetails) {

				OrderDetailVO vo = new OrderDetailVO();
				vo.setConfirmer(orderDetail.getConfirmer());
				vo.setConfirmTime(orderDetail.getConfirmTime());
				vo.setCreateTime(orderDetail.getCreateTime());
				vo.setCreator(orderDetail.getCreator());
				vo.setEditor(orderDetail.getEditor());
				vo.setEditTime(orderDetail.getEditTime());
				vo.setGdCode(orderDetail.getGdCode());
				vo.setGdName(orderDetail.getGdName());
				vo.setGiftCode(orderDetail.getGiftCode());
				vo.setGiftDetailId(orderDetail.getGiftDetailId());
				vo.setGiftId(orderDetail.getGiftId());
				vo.setGiftName(orderDetail.getGiftName());
				vo.setId(orderDetail.getId());
				vo.setIsdelete(orderDetail.getIsdelete());
				vo.setNote(orderDetail.getNote());
				vo.setOrderId(orderDetail.getOrderId());
				vo.setSalePrice(orderDetail.getSalePrice());
				vo.setSummery(orderDetail.getSummery());
				vo.setTotal(orderDetail.getTotal());

				result.add(vo);
			}
		}
		return result;
	}

	/**
	 * 订单出库方法
	 */
	@Override
	@Transactional
	public Stocks outStockByOrder(OrderInfor order) {
		// TODO Auto-generated method stub
		StocksVO stocksVo = transOrderToStock(order);
		Stocks stock = this.saveStocks(stocksVo);
		if (stock != null && stock.getId() != null) {
			return stock;
		}
		return null;
	}

	/**
	 * 由订单转换成退货单
	 * 
	 * @param order
	 * @return
	 */
	@Override
	@Transactional
	public Stocks backStockByOrder(OrderInfor order) {
		// TODO Auto-generated method stub

		StocksVO stockVo = new StocksVO();
		List<BackGoods> backGoods = backGoodService.queryByOrderId(order
				.getId());
		List<StockDetailVO> stockDetails = new ArrayList<StockDetailVO>();

		if (backGoods != null && !backGoods.isEmpty()) {
			for (BackGoods bg : backGoods) {

				Long total = backGoodService.countOrderBackGoodes(
						bg.getGiftDetailId(), order.getId());
				GiftDetail giftDetail = this.giftDetailService.getById(bg
						.getGiftDetailId());
				StockDetailVO stockDetail = new StockDetailVO();
				stockDetail.setProCode(giftDetail.getGiftCode());
				stockDetail.setProId(giftDetail.getGiftId());
				stockDetail.setProName(giftDetail.getGiftName());
				stockDetail.setTotal(total);
				stockDetail.setSalePrice(bg.getSalePrice());
				stockDetail.setSummery(bg.getSalePrice() * total);
				stockDetail.setNote(bg.getNote());
				stockDetail.setIsdelete(0);
				stockDetail.setReType(ResourceType.GIFT_RESOURCE);
				stockDetail.setRuleCode(giftDetail.getGdCode());

				stockDetail.setRuleId(bg.getGiftDetailId());
				stockDetail.setRuleName(bg.getGiftDetailName());
				stockDetail.setCreateTime(bg.getCreateTime());
				stockDetail.setEditTime(bg.getEditTime());
				stockDetail.setCreator(bg.getCreator());
				stockDetail.setEditor(bg.getEditor());
				stockDetails.add(stockDetail);
			}

			if (order != null && order.getId() != null) {

				stockVo.setStockCode(seqService.getNextSequence(Stocks.class));
				stockVo.setAction(StocksAction.BACK_STOCKS);
				stockVo.setReType(ResourceType.GIFT_RESOURCE);
				stockVo.setSummery(order.getSummery());
				stockVo.setEditor(order.getNote());
				stockVo.setCreateTime(dao.getDate());
				stockVo.setEditTime(dao.getDate());
				stockVo.setComfirmTime(dao.getDate());
				stockVo.setIsFromOrder(new Integer(1));
				stockVo.setOrderId(order.getId());
				stockVo.setOrderCode(order.getOrderCode());
				stockVo.setCreator(order.getSender());
				stockVo.setEditor(order.getSender());
				stockVo.setDetails(stockDetails);
				stockVo.setStatus(new Integer(0));
				stockVo.setIsdelete(new Integer(0));

			}

			Stocks stock = this.saveStocks(stockVo);
			if (stock != null && stock.getId() != null) {
				return stock;
			}
		}

		return null;
	}

}
